$(function(){
    
    var chatLogRef = new Firebase(FIRE_BASE_APP_LINK + 'storylog/' + PROJECT_ID + '/');
    chatLogRef.on('child_added', function(snapshot) {
        var msgData = snapshot.val();
        var login_name = msgData.login_name;
        var proj_id = msgData.proj_id;
        var story_note = msgData.story_note;
        var story_id = msgData.story_id;
        
        //console.log(snapshot.val());
        
        var proejctcnt = '<tr>' +
                            '<td>'+ login_name +'</td>' +
                            '<td>'+ story_note +'</td>' +
                            '<td class="time_slot_'+story_id+'"></td>' +
                            '<td></td>' +
                        '</tr>';
        
        $('.projectcontainer').append(proejctcnt);
        
	});
        
      var timeLogRef = new Firebase(FIRE_BASE_APP_LINK + 'timelog/' + PROJECT_ID + '/');
        timeLogRef.on('child_added', function(snapshot) {
        var msgData = snapshot.val();
        var login_name = msgData.login_name;
        var proj_id = msgData.proj_id;
        var story_note = msgData.story_note;
        var time_slot = msgData.time_slot;
        var story_id = msgData.story_id;
           
        //console.log(snapshot.val());
        
        var TimeData = time_slot;
        
        $('.time_slot_'+story_id+'').html(TimeData);
        
	});
    
    
    
        /*
	chatLogRef.on('child_added', function(snapshot) {
		var msgData = snapshot.val();
		var msg = msgData.chatmsg;
		var u_id = msgData.u_id;
		var chatclass = '';
	
		var AVTAR_IMG = '';
	
		if ( u_id == U_ID.toString()) {
			chatclass = 'chat-out';
			AVTAR_IMG = AVTAR;
		} else {
			chatclass = 'chat-in';
			AVTAR_IMG = 'avatar'+ u_id +'.png';
		}
		content_chated = $('.cm-content-chats'),
		new_chat = '<div class="'+ chatclass +'">'
                    +    '<div class="chat-avatar">'
                    +        '<img src="assets/app/img/'+ AVTAR_IMG +'" title="" />'
                    +    '</div>'
                    +    '<div class="chat-msg">'
                    +        '<p>'+ msg +'</p>'
                    //+        '<time datetime="2013-12-13T20:00">Now</time>'
                    +    '</div>'
                    +'</div>';
		content_chated.append(new_chat);
		
		
		// set height for new_chat
		var new_chat_out = $('.cm-content-chats > div[class*="chat-"]').last(),
			new_chat_height = new_chat_out.find('.chat-msg').innerHeight();

		new_chat_out.css({
			'height': new_chat_height + 'px'
		});
		
		var scrollBottom = content_chated.scrollTop() + content_chated.height();
		content_chated.animate({ scrollTop: scrollBottom }, 300);
	
		//$('.cm-content-chats').animate({ scrollTop: $(this).scrollTop() + $(this).height() }, 300);
	
	});
	
	
	// demo chat
    
    
	
	
	// manually adding height of chat-in/out buble
    $('.chat-in, .chat-out').each(function(){
        var $this = $(this),
            msg_box_height = $this.find('.chat-msg').innerHeight();

        if (msg_box_height > 40) {  // couse min-height is 40px
            $this.css({
                'height' : msg_box_height + 'px'
            })
        };
    })

    // demo sending chat
    $('#storylistingfrm').on('submit', function(e){
       
        e.preventDefault();
       

        var $this = $(this),
            content_chats = $('.cm-content-chats'),
            msg = $('#story_note').val(),
            U_ID = $('#login_id').val(),
            project_id = $('#proj_id').val(),
            //msg = "Hello",
            new_chat = '<div class="chat-out">'
                    +    '<div class="chat-avatar">'
                    +        '<img src="assets/app/img/avatar4.png" title="" />'
                    +    '</div>'
                    +    '<div class="chat-msg">'
                    +        '<p>'+ msg +'</p>'
                    +    '</div>'
                    +'</div>';

        if ($.trim(msg) != '') {
			chatLogRef.push({chatmsg: msg,u_id: user_id,project_id: project_id});
            // set height for new_chat
            var new_chat_out = $('.cm-content-chats > div[class*="chat-"]').last(),
                new_chat_height = new_chat_out.find('.chat-msg').innerHeight();

            new_chat_out.css({
                'height': new_chat_height + 'px'
            });

            var scrollBottom = content_chats.scrollTop() + content_chats.height();
            content_chats.animate({ scrollTop: scrollBottom }, 300);

            // back to first rule
            $('#message').val('');
        }
        else{
        	// back to first rule
            $('#message').val('');
        }
    })*/

});